//
//  UIImage+size.m
//  必胜课
//
//  Created by Damon on 15/10/28.
//  Copyright © 2015年 Damon. All rights reserved.
//

#import "UIImage+size.h"

@implementation UIImage (size)
//对图片尺寸进行压缩--
- (UIImage *)imageScaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}

- (UIImage *)imageByScalingToSize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        // center the image
        if (widthFactor < heightFactor) {
            
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        } else if (widthFactor > heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    // this is actually the interesting part:
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    return newImage ;
}

/** 处理截取横照片 */
- (UIImage *)imageWithEditRect:(CGRect)editRect
{
    CGFloat scale = [[UIScreen mainScreen] scale];
    CGFloat x = editRect.origin.x*scale;
    CGFloat y = editRect.origin.y*scale;
    CGFloat w = editRect.size.width*scale;
    CGFloat h = editRect.size.height*scale;
    
    CGRect clipRect = CGRectMake(x, y, w, h);
    
    // 生成新图片
    CGImageRef sourceImageRef = self.CGImage;
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, clipRect);
    UIImage *image = [UIImage imageWithCGImage:newImageRef scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(newImageRef);
    return image;
}

@end
